import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:flutter_module/map/models/ni_raster_tile_layer_model.dart';
import 'dart:convert' show json, jsonDecode;
import 'package:flutter_module/system/log_util.dart';

import 'ni_map_method_id.dart';

class NILayerManagerDispatcher {
  ///
  /// 在地图上添加RasterTileLayer
  /// [url] 地址
  /// [tilePath] 路徑
  /// [cache] 是否緩存
  Future<dynamic> switchRasterTileLayer(MethodChannel mapChannel,
      {@required String? url,
        @required String? tilePath,
        bool? cache=false,
      }) async {
    try {
       if(url !=null&&tilePath!=null){
         var result = await mapChannel.invokeMethod(
             NILayerManagerMethodId.kMapAddRasterTileLayerMethod,
             {
               'cache': cache,
               'url': url,
               'tilePath': tilePath,
             } as dynamic);
         return result;
       }
    } on PlatformException catch (e) {
      LogUtil.instance.printf(e.toString());
      return null;
    }
  }

  Future<bool?> removeRasterTileLayer(MethodChannel mapChannel, String url) async {
    return await mapChannel.invokeMethod(
        NILayerManagerMethodId.kMapAddRasterTileLayerMethod, {"url": url}) as bool;
  }

  Future<List<NIRasterTileLayer>> getRasterTileLayerList(MethodChannel mapChannel) async {

    var res =  await mapChannel.invokeMethod(NILayerManagerMethodId.kMapGetRasterTileLayerListMethod,);

    if (res!=null&&res is String) {

      var listDynamic = jsonDecode(res);

      List<Map<String, dynamic>> listMap = new List<Map<String, dynamic>>.from(listDynamic);

      List<NIRasterTileLayer> list = [];

      listMap.forEach((m) => list.add(NIRasterTileLayer.fromMap(m)));

      return list;
    }
    return [];
  }

}
